<template>
  <div>
      <!-- 查询区域 -->
      <el-form :inline="true" :model="queryForm" class="demo-form-inline" style="border:1px solid #DCDFE6;padding: 10px;">
        <el-form-item label="用户名">
            <el-input v-model="queryForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="访问路径">
            <el-input v-model="queryForm.url" placeholder="请输入访问路径"></el-input>
        </el-form-item>
        <el-form-item label="ip地址">
            <el-input v-model="queryForm.ip" placeholder="请输入ip地址"></el-input>
        </el-form-item>
        <el-form-item label="访问时间">
            <el-col :span="18" >
                <el-date-picker 
                type="date" 
                placeholder="选择日期" 
                v-model="queryForm.time" 
                value-format="yyyy-MM-dd"
                style="width: 100%;"></el-date-picker>
            </el-col>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
       </el-form>
       <!-- 显示区域 -->
       <el-container style="border:1px solid #DCDFE6;padding: 10px;margin-top:10px">
           <el-table
            :data="logsList"
            stripe
            style="width: 100%">
            <el-table-column
            label="id"
            width="140">
            <template slot-scope="scope">
                <span >{{ scope.row.id }}</span>
            </template>
            </el-table-column>
            <el-table-column
            label="用户名"
            width="180">
            <template slot-scope="scope">
                 <span>{{scope.row.username}}</span>
            </template>
            </el-table-column>
             <el-table-column
            label="访问路径"
            width="500">
            <template slot-scope="scope">
                 <span>{{scope.row.url}}</span>
            </template>
            </el-table-column>
            <el-table-column
            label="IP地址"
            width="160">
            <template slot-scope="scope">
                 <span>{{scope.row.ip}}</span>
            </template>
            </el-table-column>
            <el-table-column
            label="访问时间"
            width="175">
            <template slot-scope="scope">
                 <span>{{scope.row.time}}</span>
            </template>
            </el-table-column>
            
            <el-table-column label="操作"  width="120">
                <template slot-scope="scope">
                    <el-popover
                    placement="top"
                    width="140"
                    trigger="click"
                    v-model="scope.row.visible">
                    <p>您确定要删除吗？</p>
                    <div style="text-align: right; margin: 0">
                        <el-button size="mini" type="text" @click="scope.row.visible = false">取消</el-button>
                        <el-button type="primary" size="mini" @click="handleDelete(scope.$index, scope.row)">确定</el-button>
                    </div>
                    <el-button 
                    style="margin-left:20px"
                    slot="reference"
                    size="mini"
                    type="danger">删除</el-button>
                    </el-popover>
                </template>
            </el-table-column>
        </el-table>
       </el-container>
       <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="dataSize">
        </el-pagination>
  </div>
</template>

<script>
import Qs from 'qs';
export default {
    data(){
        return{
            dataSize:400,// 数据总数
            queryForm:{//查询表单
                username:"",
                url:"",
                ip:"",
                time:"",
                current:1,
                size:10
            },
            saveForm:{//添加或编辑表单

            },
            //所有被显示是菜单数据
            logsList: []
        }
    },
    created(){
        this.onSubmit();
    },
    methods:{
        //提交查询
        async onSubmit() {
            const res = await this.$http.get("/accesslog/listAccessLogByPage",{params:this.queryForm})
             if(res.data.code == 200){
                 this.logsList=res.data.data.records;
                 this.dataSize =res.data.data.total;
             }else{
                 this.$message.error(res.data.msg);
             }
        },
        // 删除按钮
        async handleDelete(index,row){
            row.visible = false;
            // 通过删除按钮根据id删除菜单数据
            const res = await this.$http.post("/accesslog/deleteAccessLog",Qs.stringify({id:row.id}))
            if(res.data.code === 200){
                this.$message({
                    message: res.data.msg,
                    type: 'success'
                });
                this.onSubmit();
            }else{
                this.$message.error(res.data.msg);
            }
        },
        // 每叶总数改变
        handleSizeChange(val) {
            this.queryForm.size = val;
            this.onSubmit();
        },
        //当前页
        handleCurrentChange(val) {
            this.queryForm.current = val;
            this.onSubmit();
        }
        
    }
}
</script>

<style>

</style>